<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="660px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="品名" prop="name" style="width: 100%">
            <el-input v-model="form.name" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格型号" prop="sku" style="width: 100%">
            <el-input v-model="form.sku" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品牌" prop="brand" style="width: 100%">
            <el-input v-model="form.brand" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位" prop="unit" style="width: 100%">
            <el-input v-model="form.unit" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="个数" prop="num" style="width: 100%">
            <el-input v-model="form.num" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="价格" prop="price" style="width: 100%">
            <el-input v-model="form.price" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="参数" prop="parameter" style="width: 100%">
            <el-input v-model="form.parameter" type="textarea" :rows="4" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="22">
          <el-form-item label="备注" prop="remark" style="width: 100%">
            <el-input v-model="form.remark" type="textarea" :rows="4" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { addInventory, updInventory } from '@/api/material/inventory'
export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      rules: {
        num: [{ required: true, trigger: 'blur', message: '个数不可为空' }],
        name: [{ required: true, trigger: 'blur', message: '名称不可为空' }],
        // brand: [{ required: true, trigger: 'blur', message: '品牌不可为空' }],
        sku: [{ required: true, trigger: 'blur', message: '规格不可为空' }],
        // parameter: [{ required: true, trigger: 'blur', message: '参数不可为空' }],
        unit: [{ required: true, trigger: 'blur', message: '单位不可为空' }],
        price: [{ required: true, trigger: 'blur', message: '价格不可为空' }],
        sup_id: [{ required: true, trigger: 'blur', message: '供应商不可为空' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          if (this.info.type === 'add') {
            addInventory(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('添加成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            updInventory(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
